package main.java.indi.zyj.hot100;

import main.java.indi.zyj.hot100.base.ListNode;

public class _23_detectCycle {
    public ListNode detectCycle(ListNode head) {

        ListNode fast = head, slow = head;

        while (true) {
            fast = fast.next.next;
            slow = slow.next;
            if (fast == slow) {
                break;
            }
        }

        slow = head;

        while (slow != fast) {
            slow = slow.next;
            fast = fast.next;
        }

        return slow;

    }
}
